package com.work.yshop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.work.yshop.R;
import com.work.yshop.model.Article;

import java.util.ArrayList;
import java.util.List;

/**
 * 文章列表适配器
 */
public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ArticleViewHolder> {
    
    private List<Article> articles = new ArrayList<>();
    private OnItemClickListener onItemClickListener;
    private Context context;
    
    public ArticleAdapter(Context context) {
        this.context = context;
    }
    
    public void setData(List<Article> articles) {
        this.articles = articles;
        notifyDataSetChanged();
    }
    
    public Article getItem(int position) {
        if (position >= 0 && position < articles.size()) {
            return articles.get(position);
        }
        return null;
    }
    
    @NonNull
    @Override
    public ArticleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_article, parent, false);
        return new ArticleViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull ArticleViewHolder holder, int position) {
        Article article = articles.get(position);
        
        holder.tvTitle.setText(article.getTitle());
        holder.tvSummary.setText(article.getSummary());
        holder.tvAuthor.setText("作者: " + article.getAuthor());
        holder.tvPublishDate.setText(article.getPublishDate());
        holder.tvViewCount.setText(article.getViewCount() + "阅读");
        
        // 在实际应用中，这里会使用Glide加载文章封面图片
        // Glide.with(context).load(article.getImageUrl()).into(holder.ivCover);
        
        holder.itemView.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(position);
            }
        });
    }
    
    @Override
    public int getItemCount() {
        return articles.size();
    }
    
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }
    
    public interface OnItemClickListener {
        void onItemClick(int position);
    }
    
    static class ArticleViewHolder extends RecyclerView.ViewHolder {
        ImageView ivCover;
        TextView tvTitle;
        TextView tvSummary;
        TextView tvAuthor;
        TextView tvPublishDate;
        TextView tvViewCount;
        
        public ArticleViewHolder(@NonNull View itemView) {
            super(itemView);
            ivCover = itemView.findViewById(R.id.iv_cover);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvSummary = itemView.findViewById(R.id.tv_summary);
            tvAuthor = itemView.findViewById(R.id.tv_author);
            tvPublishDate = itemView.findViewById(R.id.tv_publish_date);
            tvViewCount = itemView.findViewById(R.id.tv_view_count);
        }
    }
} 